package com.ytsky.hctechnologysys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ytsky.hctechnologysys.dao.EmployeeMapper;
import com.ytsky.hctechnologysys.entity.Employee;
import com.ytsky.hctechnologysys.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Dell
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public boolean append(Employee employee) {
        return employeeMapper.insert(employee) > 0;
    }

    @Override
    public Employee findByNamePass(String username, String password){

        return  employeeMapper.selectByNamePass(username,password);
    }


    @Override
    public PageInfo<Employee> findAll(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        final List<Employee> employees = employeeMapper.selectAll();
        PageInfo<Employee> pageInfo = new PageInfo<>(employees);
        return pageInfo;
    }

    @Override
    public PageInfo<Employee> findByKeyword(int pageNum, int pageSize, String keyword) {
        PageHelper.startPage(pageNum,pageSize);
        List<Employee> employees = employeeMapper.selectByKeyword(keyword);
        PageInfo<Employee>  pageInfo = new PageInfo<>(employees);
        return pageInfo;
    }

    @Override
    public Employee findById(int id) {
        return employeeMapper.selectById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean modify(Employee employee) {
        return employeeMapper.updateById(employee) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean remove(int id) {
        return employeeMapper.deleteById(id) > 0;
    }
}
